/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.game.math.view.action;

import java.awt.event.ActionEvent;
import java.util.Observable;
import java.util.Observer;
import org.game.math.view.config.Configure;

/**
 *
 * @author nmduc
 */
abstract class DefaultOperatorAction(name:String, operator:Enumeration#Value, configure:Configure) extends AbstractToggleAction(name, configure) with Observer {

  this.updateEnable(configure);
  this.updateSelected(configure);

  private def updateEnable(configure:Configure):Unit = {
    val operators:Set[Enumeration#Value] = configure.getOperators();
    this.setEnabled(!configure.isChosenMultiTable() && (operators.size > 1 || !operators.contains(this.operator)));
  }

  private def updateSelected(configure:Configure):Unit= {
    val operators:Set[Enumeration#Value] = configure.getOperators();
    this.setSelected(operators.contains(this.operator));
  }

  override final def actionPerformed(ae:ActionEvent):Unit = {
    if (this.isSelected()) {
      Configure.getInstance().addOperator(this.operator);
    } else {
      Configure.getInstance().removeOperator(this.operator);
    }
  }

  override def update( o:Observable, arg:Any):Unit = {
    val configure:Configure = o.asInstanceOf[Configure];
    this.updateEnable(configure);
  }
}
